Overview of the Pipe Operator %>%

While you aren't required to ever use the pipe operator with dplyr or tidyr, it can be very useful when trying to perform multiple operations/functions on a data set. The pipe operator will allow you to avoid either a long nested operation or doing a bunch of assignmets.

In its most basic form, this is what an example of using %>% with dplyr looks like:

The Data

In [9]:
df <- mtcars
library(dplyr)

Using Nesting

In [16]:
library(dplyr)
arrange(sample_n(filter(df,mpg>20),size=5),desc(mpg))
Out[16]:
mpgcyldisphpdratwtqsecvsamgearcarb
127.3479664.081.93518.91141
2264120.3914.432.1416.70152
324.44146.7623.693.19201042
422.84140.8953.923.1522.91042
52161601103.92.87517.020144

Using Multiple Assignments

In [7]:
library(dplyr)
a <- filter(df,mpg > 20)
b <- sample_n(a,size = 5)
c <- arrange(b,desc(mpg))
c
Out[7]:
mpgcyldisphpdratwtqsecvsamgearcarb
130.4495.11133.771.51316.91152
230.4475.7524.931.61518.521142
324.44146.7623.693.19201042
421.54120.1973.72.46520.011031
521.441211094.112.7818.61142

Using the Pipe Operator

In [17]:
library(dplyr)
df %>% filter(mpg > 20) %>% sample_n(size = 5) %>% arrange(desc(mpg))
Out[17]:
mpgcyldisphpdratwtqsecvsamgearcarb
130.4475.7524.931.61518.521142
2264120.3914.432.1416.70152
324.44146.7623.693.19201042
422.84108933.852.3218.611141
52161601103.92.6216.460144

Hope you find this helpful when performing various operations on your data!